package com.itheima.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContextDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          //1.获取ServletContext对象
        ServletContext context = getServletContext();
        //2.常用方法演示
        //获取全局配置参数  根据key获得value
        String value = context.getInitParameter("globalDesc");
        System.out.println(value);
        //获取应用的虚拟目录
        String contextPath = context.getContextPath();
        System.out.println(contextPath);
        //根据虚拟目录获取绝对路径
        String realPath = context.getRealPath("/");
        System.out.println(realPath);
        //设置共享数据
        context.setAttribute("username","zhangsan");

        //删除共享数据
        context.removeAttribute("username");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
